import { request } from '@umijs/max';
import type { Tag } from '../tags/typing';
import type { DataResponse, MsgResponse, PagedDataResponse } from '../typing';
import type {
  ArticleDetail,
  ArticleSimpleInfo,
  ArticleTag,
  QueryArticleCondition,
  SaveArticleInfo,
} from './typing';

// 保存文章
export function saveArticle(data: SaveArticleInfo) {
  return request<Promise<MsgResponse>>('/api/articles', {
    method: 'POST',
    data,
  });
}

// 编辑文章
export function updatedArticle(article_id: number, data: SaveArticleInfo) {
  return request<Promise<MsgResponse>>(`/api/articles/${article_id}`, {
    method: 'PATCH',
    data,
  });
}

// 获取标签列表
export function getTagsList() {
  return request<Promise<PagedDataResponse<Tag>>>('/api/tags', {
    method: 'GET',
  });
}

// 筛选文章
export function queryArticles(queryCondition: QueryArticleCondition) {
  return request<Promise<PagedDataResponse<ArticleSimpleInfo | ArticleDetail>>>(
    '/api/articles',
    {
      method: 'GET',
      params: queryCondition,
    },
  );
}

// 删除文章
export function deleteArticle(id: number) {
  return request<Promise<MsgResponse>>(`/api/articles/${id}`, {
    method: 'DELETE',
  });
}

// 获取单个文章
export function getArticle(article_id: number) {
  return request<Promise<DataResponse<ArticleDetail>>>(
    `/api/articles/${article_id}`,
    {
      method: 'GET',
    },
  );
}

// 获取文章标签
export function getArticleTags(article_id: number) {
  return request<Promise<PagedDataResponse<ArticleTag>>>(
    `/api/article-tag/${article_id}`,
    {
      method: 'GET',
    },
  );
}
